<?php
namespace app\user\controller;
use think\Input;
use app\admin\model\Users;
use think\Lang;
use think\Cookie;
use app\common\controller\Base;
class Index extends Common{
    public function _initialize(){
        parent::_initialize();
        
    }
    public function index(){
        $this->assign('title','用戶登录');
        if(request()->isPost()) {
            $admin = new Users();
            $data = input('post.');
            // if(!$this->check($data['captcha'])){
            //     return json(array('code' => 0, 'msg' => lang('Verification code error')));
            // }
            // if(empty($data['email']) || !is_email($data['email'])){
            //     return json(array('code' => 0, 'msg' => lang('Please enter the mailbox address')));
            // }
            $num = $admin->login($data);
            if($num == 1){
                return json(['code' => 1, 'msg' => lang('Log on successfully!'), 'url' => url('admin/index/index')]);
            }else {
                if($num==-2){
                    return json(array('code' => 0, 'msg' =>lang('The user has been disabled!')));
                }
                return json(array('code' => 0, 'msg' => lang('User name or password error, re - input!')));
            }
        }
        return $this->fetch('login/index');
    }
    public function check($code){
        if (!captcha_check($code)) {
            return false;
        } else {
            return true;
        }
    }
}